package com.taxshare.seller.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.pvalidate.ParamValid;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.AlsCancelDetailLogEntity;
import com.taxshare.seller.entity.AlsCancelLogEntity;
import com.taxshare.seller.entity.SellerInvoiceDetailEntity;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.seller.service.AlsCancelLogService;
import com.taxshare.seller.service.DocumentInfoDetailService;
import com.taxshare.seller.service.InvoicedService;
import com.taxshare.seller.service.OpenInvoiceService;
import com.taxshare.seller.view.dto.*;
import com.taxshare.seller.view.vo.SellerInvoiceRespVO;
import com.taxshare.seller.view.vo.UpdateSellerInvoiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author liupp
 * @ClassName: OpenInvoiceController
 * @Description: (待开)
 * @date 2021年4月8日
 */
@Slf4j
@RestController
@RequestMapping("/openInvoice")
@Api(tags = "待开发票")
public class OpenInvoiceController {

    @Autowired
    InvoicedService invoicedService;

    @Autowired
    OpenInvoiceService openInvoiceService;

    @Autowired
    AlsCancelLogService ALSCancelLogService;

    @Autowired
    DocumentInfoDetailService documentInfoDetailService;

    @PostMapping("/getSubDataByPage")
    @ApiOperation(value = "待开发票查询", notes = "待开发票管理")
    public Response<IPage<SellerInvoiceEntity>> getSubDataByPage(@RequestBody PageRequest<InvoicedDTO> query) {
        return openInvoiceService.getSubDataByPage(query);
    }

    @ParamValid
    @PostMapping("/queryDetail")
    @ApiOperation(value = "待开发票详情查询", notes = "待开发票管理")
    public Response<IPage<SellerInvoiceDetailEntity>> queryDetail(@RequestBody @Valid PageRequest<InvoicedDTO> query) {
        return invoicedService.queryDetail(query);
    }

    @GetMapping("/queryInvoiceImage")
    @ApiOperation(value = "获取发票影像", notes = "待开发票管理")
    public Response<SellerInvoiceRespVO> queryInvoiceImage(@RequestParam String serialNo, HttpServletResponse response) throws IOException {
        return invoicedService.queryInvoiceImage(serialNo, response);
    }

    @PostMapping("/upInvoiceRemark")
    @ApiOperation(value = "根据发票流水号修改备注", notes = "待开发票管理")
    public Response upInvoiceRemark(@RequestBody SellerInvoiceDetailDTO parames) {
        return openInvoiceService.upInvoiceRemark(parames);
    }

    @PostMapping("/synchroOpenInvoice")
    @ApiOperation(value = "开具发票", notes = "待开发票管理")
    public Response synchroOpenInvoice(@RequestBody List<String> serialNos) {
        return openInvoiceService.synchroOpenInvoice(serialNos);
    }

    @GetMapping("/invoicedIdsQuery")
    @ApiOperation(value = "根据票据GUID获取发票流水号", notes = "待开发票管理")
    public Response invoicedIdsQuery(@RequestParam String docId) {
        return invoicedService.invoicedIdsQuery(docId);
    }

    @PostMapping("/upSellerInvoice")
    @ApiOperation(value = "修改发票及其明细信息", notes = "待开发票管理")
    public Response upSellerInvoice(@RequestBody UpdateSellerInvoiceVO updateSellerInvoiceVo) {
        return invoicedService.upSellerInvoice(updateSellerInvoiceVo);
    }

    @PostMapping("/updateInvoiceType")
    @ApiOperation(value = "修改发票类型", notes = "待开发票管理")
    public Response updateInvoiceType(@RequestBody UpdateInvoiceTypeDTO updateInvoiceTypeDTO) {
        return openInvoiceService.updateInvoiceType(updateInvoiceTypeDTO);
    }

    @PostMapping("/cancelInvoice")
    @ApiOperation(value = "驳回发票（als）", notes = "待开发票管理")
    public Response cancelInvoice(@RequestBody AlsCancelInvoiceDTO dto) {
        return openInvoiceService.cancelInvoice(dto);
    }

    @PostMapping("/queryStock")
    @ApiOperation(value = "设备状态，余票查询", notes = "待开发票管理")
    public Response queryStock(@RequestParam String sellerNo) {
        return openInvoiceService.queryStock(sellerNo);
    }

    @PostMapping("/queryInvoiceCancel")
    @ApiOperation(value = "待开已作废发票查询", notes = "待开发票管理")
    public Response<IPage<AlsCancelLogEntity>> getInvoiceCancelByPage(@RequestBody PageRequest<InvoicedDTO> query) {
        return ALSCancelLogService.getInvoiceCancelByPage(query);
    }

    @PostMapping("/queryCancelDetail")
    @ApiOperation(value = "待开已作废发票订单明细查询", notes = "待开发票管理")
    public Response<List<AlsCancelDetailLogEntity>> queryCancelDetail(@RequestParam String serialNo) {
        return ALSCancelLogService.queryCancelDetail(serialNo);
    }

    @PostMapping("/syncNextInvoice")
    @ApiOperation(value = "获取当前开票的发票代码和号码", notes = "发票库存管理")
    public Response syncNextInvoice(@RequestBody InvoicedDTO blankInvoiceCollctionDTO) throws Exception {
        return openInvoiceService.syncNextInvoice(blankInvoiceCollctionDTO);
    }

    @PostMapping("/queryAllDetail")
    @ApiOperation(value = "发票预览明细查询" , notes = "发票明细预览")
    public Response queryAllDetail(@RequestBody PageRequest<DocumentInvoiceDetailDTO> query){
        return documentInfoDetailService.queryAllDetail(query);
    }

    @PostMapping("/addDetail")
    @ApiOperation(value = "发票预览明细新增" , notes = "发票明细预览")
    public Response addDetail(@RequestBody DocumentInvoiceDetailDTO info){
        return documentInfoDetailService.addDetail(info);
    }

    @PostMapping("/updateDetail")
    @ApiOperation(value = "发票预览明细更新" , notes = "发票明细预览")
    public Response updateDetail(@RequestBody DocumentInvoiceDetailDTO info){
        return documentInfoDetailService.updateDetail(info);
    }

    @PostMapping("/deleteDetail")
    @ApiOperation(value = "发票预览明细删除" , notes = "发票明细预览")
    public Response deleteDetail(@RequestBody DocumentInvoiceDetailDTO info){
        return documentInfoDetailService.deleteDetail(info);
    }
}
